<?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Date: 2017/1/12
 * Time: 14:19
 */

namespace App\Admin\Controllers;
use App;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Table;
use App\Models\Xjb\TestingXjbUserRecord;
use App\Models\TestingDetail;
use App\Models\TestingItem;
use App\Services\MyPermission;
use TestingXjbHelper;
use XjbUserRecordService;
use TestingDetailService;
class TestingXjbAnswersController extends Controller
{
    use ModelForm;

    public function index(){
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.testing_xjb_record.title.list'));
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */

    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header();
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(TestingXjbUserRecord::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->display('nick_name', trans('backend.user_record.column.uid'));
            $form->display('testing_name',trans('backend.user_record.column.tid'));
            $form->text('score',trans('backend.user_record.column.score'))->rules('required');
            $form->text('conclusion', trans('backend.user_record.column.conclusion'))->rules('required');
        });
    }

    protected function grid(){
        return Admin::grid(TestingXjbUserRecord::class, function (Grid $grid) {
            $grid->model()->orderBy('created_at', 'desc');
            $this->buildGrid($grid);
        });
    }

    protected function buildGrid($grid){
        $grid->id('ID')->sortable();
        $grid->uid(trans('backend.testing_xjb_record.column.uid'));
        $grid->column('testing_name',trans('backend.testing_xjb_record.column.tid'));
        $grid->score(trans('backend.testing_xjb_record.column.score'));
        $grid->conclusion(trans('backend.testing_xjb_record.column.conclusion'));
        $grid->created_at(trans('backend.testing_xjb_record.column.creat_at'));
        $grid->rows(function($row){
            $testingDetail = TestingDetailService::getById($row->tid);
            $name = $testingDetail['name'];
            $row->column('testing_name',$name);

            if(MyPermission::check('edit') || MyPermission::check('delete')) {
                if(MyPermission::check('edit') && MyPermission::check('delete')){
                    $row->actions('edit|delete');
                }elseif(MyPermission::check('edit') && !MyPermission::check('delete')){
                    $row->actions('edit');
                }elseif(!MyPermission::check('edit') && MyPermission::check('delete')){
                    $row->actions('delete');
                }
            }else{
                $row->actions('');
            }

            $row->actions()->add(function ($row) {
                return "<a href='/admin/testing/xjb/answersInfo/{$row->id}/{$row->tid}'><i class='fa fa-eye'></i></a>";
            });
        });
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->like('uid', trans('backend.testing_xjb_record.column.uid'));
        });
        $grid->disableExport();
        $grid->disableCreation();
        if(!MyPermission::check('delete')){
            $grid->disableBatchDeletion();
        }
    }


    public function info($id,$tid)
    {
        return Admin::content(function (Content $content) use ($id,$tid) {
            $content->header(trans('backend.testing_xjb_record.title.info'));
            $factor = $this -> formatConclusion($id,$tid);
            $str = "";
            if(count($factor['increase_factor'])>0){
                foreach($factor['increase_factor'] as $k => $v){
                    $str =$str.$v['tag']."</br>";
                }
            }else{
                $str =$str.'无';
            }
            $box = new Box(trans('backend.testing_xjb_record.title.increase'),$str);
            $content->row($box->style('info')->solid());

             $strs = "";
            if(count($factor['lower_factor'])>0){
                foreach($factor['lower_factor'] as $k => $v){
                    $strs =$strs.$v['tag']."</br>";
                }
            }else{
                $strs =$strs.'无';
            }
            $box = new Box(trans('backend.testing_xjb_record.title.lower'),$strs);
            $content->row($box->style('info')->solid());

            $title = TestingDetail::where('id',$tid)->value('name');
            $headers = ['题号','测评题目','提交答案'];
            $result = $this->formate($id,$tid);
            $answerstr = [];
            foreach($result as $k=>$v){
                $answer = implode(',',$v);
                $answerArr = explode(',',$answer);
                $answerstr[$k]=$answerArr;
            }
            $table = new Table($headers,$answerstr);
            $content->row((new Box($title,$table))->style('info')->solid());
        });
    }

    protected function formate($id,$tid){
        $res= TestingXjbUserRecord::select('answers')->where(['id'=>$id,'tid'=>$tid])->first()->toarray();
        $answerArray =  TestingXjbHelper::strToArr($res['answers']);
        $indexArr = [];
        foreach($answerArray as $k => $v){
            $result = TestingItem::select('title','question_type')->where(['testing_id'=>$tid,'index'=>$v['index']])->first()->toarray();
            $indexArr[$k]['index'] = $v['index'];
            $indexArr[$k]['title'] = $result['title'];
            $indexArr[$k]['answer']= $v['answer'];
        }
        return $indexArr;
    }

    protected function formatConclusion($id,$tid){
        $result = TestingXjbUserRecord::select('final_answers','answers')->where(['id'=>$id,'tid'=>$tid])->first()->toarray();
        $answerArr = TestingXjbHelper::strToArr($result['answers']);
        $data = TestingXjbHelper::getSingleFactor($answerArr,$tid,$result['final_answers']);
        return $data;
    }

}